//
// DotNetNuke - http://www.dotnetnuke.com
// Copyright (c) 2002-2010
// by Perpetual Motion Interactive Systems Inc. ( http://www.perpetualmotion.ca )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Users;
using DotNetNuke.Framework;
using DotNetNuke.UI.WebControls;
namespace DotNetNuke.Modules.Admin.Users
{
	partial class Membership : UserModuleBase
	{
		public event EventHandler MembershipAuthorized;
		public event EventHandler MembershipPasswordUpdateChanged;
		public event EventHandler MembershipUnAuthorized;
		public event EventHandler MembershipUnLocked;
		public UserMembership UserMembership
		{
			get
			{
				UserMembership _Membership = null;
				if (User != null)
				{
					_Membership = User.Membership;
				}
				return _Membership;
			}
		}
		public void OnMembershipAuthorized(EventArgs e)
		{
			if (MembershipAuthorized != null)
			{
				MembershipAuthorized(this, e);
			}
		}
		public void OnMembershipPasswordUpdateChanged(EventArgs e)
		{
			if (MembershipPasswordUpdateChanged != null)
			{
				MembershipPasswordUpdateChanged(this, e);
			}
		}
		public void OnMembershipUnAuthorized(EventArgs e)
		{
			if (MembershipUnAuthorized != null)
			{
				MembershipUnAuthorized(this, e);
			}
		}
		public void OnMembershipUnLocked(EventArgs e)
		{
			if (MembershipUnLocked != null)
			{
				MembershipUnLocked(this, e);
			}
		}
		public override void DataBind()
		{
			if (UserInfo.UserID == User.UserID)
			{
				cmdAuthorize.Visible = false;
				cmdUnAuthorize.Visible = false;
				cmdUnLock.Visible = false;
				cmdPassword.Visible = false;
			}
			else
			{
				cmdUnLock.Visible = UserMembership.LockedOut;
				cmdUnAuthorize.Visible = UserMembership.Approved;
				cmdAuthorize.Visible = !UserMembership.Approved;
				cmdPassword.Visible = !UserMembership.UpdatePassword;
			}
			MembershipEditor.DataSource = UserMembership;
			MembershipEditor.DataBind();
		}
		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);

			PageBase basePage = this.Page as PageBase;
			if (basePage != null)
			{
				if (basePage.PageCulture.TextInfo.IsRightToLeft)
				{
					MembershipEditor.LabelMode = LabelMode.Right;
				}
				else
				{
					MembershipEditor.LabelMode = LabelMode.Left;
				}
			}
		}
		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);
			MembershipEditor.LocalResourceFile = this.LocalResourceFile;
			cmdAuthorize.Click += new EventHandler(cmdAuthorize_Click);
			cmdPassword.Click += new EventHandler(cmdPassword_Click);
			cmdUnAuthorize.Click += new EventHandler(cmdUnAuthorize_Click);
			cmdUnLock.Click += new EventHandler(cmdUnLock_Click);
		}
		private void cmdAuthorize_Click(object sender, System.EventArgs e)
		{
			User.Membership = (UserMembership)MembershipEditor.DataSource;
			User.Membership.Approved = true;
			UserController.UpdateUser(PortalId, User);
			OnMembershipAuthorized(EventArgs.Empty);
		}
		private void cmdPassword_Click(object sender, System.EventArgs e)
		{
			User.Membership = (UserMembership)MembershipEditor.DataSource;
			User.Membership.UpdatePassword = true;
			UserController.UpdateUser(PortalId, User);
			OnMembershipPasswordUpdateChanged(EventArgs.Empty);
		}
		private void cmdUnAuthorize_Click(object sender, System.EventArgs e)
		{
			User.Membership = (UserMembership)MembershipEditor.DataSource;
			User.Membership.Approved = false;
			UserController.UpdateUser(PortalId, User);
			OnMembershipUnAuthorized(EventArgs.Empty);
		}
		private void cmdUnLock_Click(System.Object sender, System.EventArgs e)
		{
			bool isUnLocked = UserController.UnLockUser(User);
			if (isUnLocked)
			{
				User.Membership.LockedOut = false;
				OnMembershipUnLocked(EventArgs.Empty);
			}
		}
	}
}
